<template>
  <div class="comment">
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
    <!-- 评论 -->
      <van-cell v-for="(item, index) in list" :key="index" :title="item.aut_id">
        <template #title>
          <div class="cmt-wrap">
            <!-- t图片 -->
            <van-image
              width="50"
              height="50"
              fit="contain"
              cover
              :src="item.aut_photo"
            />
            <div class="info">
              <div class="name">{{ item.aut_name }}</div>
              <div class="content">{{ item.content }}</div>
              <div class="time">
                {{ item.pubdate | relvTime }}
                <van-button type="default" size="mini" @click="showReply(item)"
                  >回复（{{ item.reply_count }}）</van-button
                >
              </div>
            </div>
            <!-- 图标 -->
            <van-icon name="like-o" />
          </div>
        </template>
      </van-cell>
    </van-list>
    <!-- 输入的区域 -->
    <div class="cmt-input-wrap">
      <van-field
        v-model="cmtValue"
        center
        clearable
        placeholder="请输入你要评论的内容"
        @keyup.enter="send"
      >
        <template #button>
          <van-button @click="send" size="info" type="info" round style="width: 50px"
            >发送</van-button
          >
        </template>

      </van-field>
    </div>
<!-- 回复 -->
    <!-- 评论回复 -->
    <van-action-sheet  v-model="sheetShow" title="发表评论">
      <reply v-if="sheetShow" :currentItem="currentItem"/>
    </van-action-sheet>


  </div>
</template>

<script>
import { commentsListAPI,sendCmtAPI } from "@/api";
import reply from './reply.vue';

export default {
  components: { reply },
  name:"commenrt",
  data() {
    return {
      list: [],
      loading: false,
      finished: false,
      cmtValue: "",
      sheetShow: false,
      offset:undefined,
      currentItem:[]
    };
  },
  methods: {
   async showReply(item){
     this.sheetShow=true
           // 2. 把当前被点的评论传递给回复面板
      this.currentItem = item
     
    },

    // 回复/发送回车按钮按钮
   async send() {

    // 判断输入的评论是否为空
    if(this.cmtValue == ''){
     this.$toast('评论不能为空！')
     return
    }
      // this.sheetShow = true;
      // console.log(123);
      // 请求发表评论的数据
      let res = await sendCmtAPI({
        target:this.$route.query.id,
        content:this.cmtValue
      })
      // unshift  添加到第一条
      this.list.unshift(res.data.data.new_obj)
      // 将输入框的内容清空
      this.cmtValue=''
      this.$toast ('发表评论成功')
    },
    async onLoad() {
      let res = await commentsListAPI({
        type: "a",
        source: this.$route.query.id,
        offset: this.offset,
      });
      this.list.push(...res.data.data.results);
      // 把本次的最后一条id作为下次请求的参数
      this.offset = res.data.data.last_id;
      this.loading = false;
      //  当本次的最后一条id，等于 所有数据的结束id就意味着加载完了
      if (res.data.data.end_id == res.data.data.last_id) {
        this.finished = true;
      }
    },
  },
};
</script>

<style lang='less' scoped>
.comment {
  margin-top: 20px;

  ::v-deep .cmt-wrap {
    display: flex;
    .van-image {
      width: 45px;
      height: 45px;
    }
    .info {
      margin-left: 20px;
      flex: 1;
    }
  }
  .cmt-input-wrap {
    position: fixed;
    left: 0;
    bottom: 0;
    width: 100%;
  }
}
</style>